﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class CustomerManagement_CREDITEDIT : System.Web.UI.Page
{
    private string _code;
    public string Code
    {
        get
        {
            return Request.QueryString["code"] != null ? Request.QueryString["code"] : "";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["FCode"] = "300400114";
        if (!IsPostBack)
        {
            txtCustomerCode.Attributes.Add("onclick", "SearchVIP('../');");
            drpType.DataSource = new BLL.CREDITTYPE().GetAllList();
            drpType.DataBind();

            if (this.Code != "")
            {
                Model.CREDIT Credit = new BLL.CREDIT().GetModel(int.Parse(this.Code));
                txtCustomerCode.Text = Credit.CREDIT_CUSTOMERCODE;
                drpType.SelectedValue = Credit.CREDIT_TYPE.ToString();
                txtFS.Text = Credit.CREDIT_FS.ToString();
                if (Credit.CREDIT_CHECK == 1)
                {
                    btnSave.Enabled = false;
                    btnCheck.Enabled = false;
                    btnDelete.Enabled = false;
                }
            }
        }
    }
    private bool CheckNum(string str)
    {
        Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");
        if (reg.IsMatch(str))
        {
            return true;
        }
        return false;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Model.CREDIT credit = null;
        if (this.Code == "")
        {
            if (!Check.CheckPermission("300400114", "00001"))
            {
                Share.Alert(this.Page, "无权操作"); return;
            }
            credit = new Model.CREDIT();
            credit.CREDIT_TYPE = int.Parse(drpType.SelectedItem.Value);
            credit.CREDIT_NAME = drpType.SelectedItem.Text;
            credit.CREDIT_CUSTOMERCODE = txtCustomerCode.Text.Trim();
            credit.CREDIT_FS = decimal.Parse(txtFS.Text.Trim());
            credit.CREDIT_ADDTIME = DateTime.Now;
            credit.CREDIT_CHECK = 0;

            int ret = new BLL.CREDIT().Add(credit);
            if (ret > 0)
            {
                Response.Redirect("CREDIT.aspx");
            }
        }
        else
        {
            if (!Check.CheckPermission("300400114", "00002"))
            {
                Share.Alert(this.Page, "无权操作"); return;
            }
            Model.CREDIT ct = new BLL.CREDIT().GetModel(int.Parse(this.Code));
            if (ct != null)
            {
                if (ct.CREDIT_CHECK == 1)
                {
                    Share.Alert(this.Page, "此单据已被审核，不允许再次操作！");
                    return;
                }
            }
            credit = new BLL.CREDIT().GetModel(int.Parse(this.Code));
            credit.CREDIT_TYPE = int.Parse(drpType.SelectedItem.Value);
            credit.CREDIT_NAME = drpType.SelectedItem.Text;
            credit.CREDIT_CUSTOMERCODE = txtCustomerCode.Text.Trim();
            credit.CREDIT_FS = decimal.Parse(txtFS.Text.Trim());
            credit.CREDIT_ADDTIME = DateTime.Now;
            bool ret = new BLL.CREDIT().Update(credit);
            if (ret)
            {
                Share.Alert(this.Page, "修改成功"); return;
            }
        }
    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {

        if (!Check.CheckPermission("300400114", "00010"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        if (this.Code != "")
        {
            Model.CREDIT ct = new BLL.CREDIT().GetModel(int.Parse(this.Code));
            if (ct != null)
            {
                if (ct.CREDIT_CHECK == 1)
                {
                    Share.Alert(this.Page, "此单据已被审核，不允许再次操作！");
                    return;
                }
            }
            string limit = ConfigurationManager.AppSettings["HeiMingDan"];
            int ret = new BLL.CREDIT().Check(int.Parse(this.Code), txtCustomerCode.Text.Trim(), decimal.Parse(txtFS.Text.Trim()), decimal.Parse(limit));
            if (ret == 1)
            {
                Response.Redirect("CREDIT.aspx");
            }
            else
            {
                Share.Alert(this.Page, "操作失败，请重新审核!");
                return;
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400114", "00003"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        if (this.Code != "")
        {
            Model.CREDIT credit = new BLL.CREDIT().GetModel(int.Parse(this.Code));
            if (credit.CREDIT_CHECK==0)
            {
                bool ret = new BLL.CREDIT().Delete(int.Parse(this.Code));
                if (!ret)
                {
                    Share.Alert(this.Page, "操作失败");
                    return;
                }
                Response.Redirect("CREDIT.aspx");
            }
            
            Share.Alert(this.Page, "已经审批不能删除");
        }
    }
    protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
    {
        Model.CREDITTYPE type = new BLL.CREDITTYPE().GetModel(int.Parse(drpType.SelectedItem.Value));
        txtFS.Text = type.CREDITTYPE_FS.ToString();
    }
}
